package com.forimageandlist;

import java.util.ArrayList;
import java.util.HashMap;

//import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.realtyportal.R;
import com.jason.apps.dashboard.DeleteAccount;

public class Adapter extends BaseAdapter {
	public static ArrayList<Integer> idList=new ArrayList<Integer>();
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    HashMap<String, String> person = new HashMap<String, String>();
    
    
    public Adapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
   
	public View getView(final int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        
        if(convertView==null)
            vi = inflater.inflate(R.layout.listrowfordelete, null);

        TextView title = (TextView)vi.findViewById(R.id.dtitle); // title
        TextView artist = (TextView)vi.findViewById(R.id.dartist); // artist name
        TextView contact = (TextView)vi.findViewById(R.id.dcontact); // artist name
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.dlist_image); // thumb image
        CheckBox check=(CheckBox)vi.findViewById(R.id.checkBox1);
        
        check.setOnClickListener(new OnClickListener(){
        	
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//int i=0;
				//posArray=new int[getCount()];
				person=data.get(position);
				
				if (((CheckBox) v).isChecked()) {
					
					idList.add(Integer.parseInt(person.get(DeleteAccount.KEY_PERSON_ID)));
					//Toast.makeText(activity, "Clicked"+i+"--"+posArray[i]+"  ///Size of data is--"+posArray.length, Toast.LENGTH_SHORT).show();
					System.out.println("data from array:"+idList.toString());
				}
				else if(!((CheckBox) v).isChecked()){
					
					for (int i = 0; i < idList.size(); i++) {
					   // System.out.println(idList.get(i));
					    if(idList.get(i)==(Integer.parseInt(person.get(DeleteAccount.KEY_PERSON_ID)))){
					    idList.remove(idList.get(i));  
					    System.out.println("data from array after remove:"+idList.toString());
					    break;
					    }
					}
					
					//idList.remove();
					
				}
			

			}
        });
        
        
        person=data.get(position);
        String fname=person.get(DeleteAccount.KEY_FIRSTNAME);
        String lname=person.get(DeleteAccount.KEY_LASTNAME);
        String name=fname+" "+ lname;
        
        String mobile=person.get(DeleteAccount.KEY_MOBILE);
        String landline=person.get(DeleteAccount.KEY_LANDLINE);
        String con=mobile+" / "+ landline;
        
        title.setText(name);
        artist.setText(person.get(DeleteAccount.KEY_EMAILADDRESS));
        contact.setText(con);
        
        imageLoader.DisplayImage(person.get(DeleteAccount.KEY_IMAGE), thumb_image);
        
        return vi;

}
}
